package com.basic.library.binding.viewadapter.recyclerview;

import androidx.databinding.ObservableList;

import com.basic.library.brvah.MyBaseMultiItemQuickAdapter;
import com.basic.library.brvah.MyBaseQuickAdapter;
import com.basic.library.utils.MyLog;
import com.chad.library.adapter.base.BaseQuickAdapter;

/**
 * @author jiangyun
 * @description ObservableList多用于增删改，item位置不变，查询不该用(因为刷新接口时先clear后再addAll，视图会刷新2遍)
 * @time 创建时间：2020/2/11 22:15
 */
public class ListFactory {

    public static ObservableList.OnListChangedCallback getListChangedCallback(BaseQuickAdapter adapter) {
        return new ObservableList.OnListChangedCallback() {
            @Override
            public void onChanged(ObservableList observableList) {
                adapter.notifyDataSetChanged();
                MyLog.p("onChanged");
            }

            @Override
            public void onItemRangeChanged(ObservableList observableList, int i, int i1) {
                adapter.notifyItemRangeChanged(i, i1);
                MyLog.p("onItemRangeChanged");
            }

            @Override
            public void onItemRangeInserted(ObservableList observableList, int i, int i1) {
                MyLog.p("onItemRangeInserted" + observableList.size() + "开始下标" + i + "插入" + i1);
                initData(adapter, observableList, i1, false); //使用第三方自带
//                adapter.notifyDataSetChanged(); //无动画
            }

            @Override
            public void onItemRangeMoved(ObservableList observableList, int i, int i1, int i2) {
                if (i2 == 1) {
                    adapter.notifyItemMoved(i, i1);
                } else {
                    adapter.notifyDataSetChanged();
                }
                MyLog.p("onItemRangeMoved");
            }

            @Override
            public void onItemRangeRemoved(ObservableList observableList, int i, int i1) {
                if (observableList.size() == 0 && isClear(adapter)) { //数据size==0 ，并通过适配器clearData，表示清空
                    initData(adapter, observableList, i1, true); //使用第三方自带
                } else {
                    adapter.notifyItemRangeRemoved(i + adapter.getHeaderLayoutCount(), i1);
                }
                MyLog.p("onItemRangeRemoved" + observableList.size() + "开始下标" + i + "移除" + i1);
            }
        };
    }

    static boolean isClear(BaseQuickAdapter adapter) {
        if (adapter instanceof MyBaseQuickAdapter) {
            MyBaseQuickAdapter adapter1 = (MyBaseQuickAdapter) adapter;
            return adapter1.isClear;
        } else if (adapter instanceof MyBaseMultiItemQuickAdapter) {
            MyBaseMultiItemQuickAdapter adapter1 = (MyBaseMultiItemQuickAdapter) adapter;
            return adapter1.isClear;
        }
        return false;
    }

    static void initData(BaseQuickAdapter adapter, ObservableList list, int newDataSize, boolean clear) {
        if (adapter instanceof MyBaseQuickAdapter) {
            MyBaseQuickAdapter adapter1 = (MyBaseQuickAdapter) adapter;
            if (clear)
                adapter1.page = 1;
            if (adapter1.page == 1) {
                adapter1.setNewData(list);
            } else {
                adapter1.addDataNo(newDataSize);
            }
        } else if (adapter instanceof MyBaseMultiItemQuickAdapter) {
            MyBaseMultiItemQuickAdapter adapter1 = (MyBaseMultiItemQuickAdapter) adapter;
            if (clear)
                adapter1.page = 1;
            if (adapter1.page == 1) {
                adapter1.setNewData(list);
            } else {
                adapter1.addDataNo(newDataSize);
            }
        }
    }
}
